<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!--<jsp:useBean id="clsConn" class="utility.connections" scope="session"></jsp:useBean>-->
<%@page import = "utility.connections" %>
<script type="text/javascript" src="../../js/checkdate.js"></script>
<script type="text/javascript">
	function modify(formName)
	{
		var pattern = /\w+@\w+.\w+/;
		var number= /[0-9]/;
		var	user=document.forms[formName].elements["username"].value;
		var passwd=document.forms[formName].elements["password"].value;
		var email=document.forms[formName].elements["email"].value;
		if(document.forms[formName].elements["dataregistrazione"].value.length==0)
		{
			alert("ERROR data registrazione non inserita");
		}
		if(user.length==0)
		{
			alert("ERROR campo user name non vuoto");
			return;
		}
		if(passwd.length<8)
		{
			alert("ERROR lunghezza password inserita non sufficente");
			return;
		}
		if(!pattern.test(email))
		{	
			alert("ERROR forma campo E-mail errato");
			return;
		}
		if(confirm("Applicare le modifiche apportate?"))
			document.forms[formName].submit();	
	}
	function isEqual(string1, string2)
	{
		string1=string1.toUpperCase();
		string2=string2.toUpperCase();
		if(string1.indexOf(string2,0)<0)
			{return false;}
		else
			{return true;}
	}
	function chkData(value)
	{
		var early;
		
		if (isEqual(value,"dataregistrazione"))
		{
			if(!isDate(document.form1.dataregistrazione.value))
			{
				alert("Data non valida");
				document.form1.dataregistrazione.value="";
				document.form1.dataregistrazione.focus();
				return;
			}
			document.form1.tipologia.focus();
		}
	}
	function insData(value)
	{		
		var dat;
		var obj;
		var nxt;
		
		if(isEqual(value,"dataregistrazione"))
		{
			dat = document.form1.dataregistrazione.value;
			obj = document.form1.dataregistrazione;
			nxt = document.form1.tipologia;
		}
		
		if(dat.length==2||dat.length==5)
		{
			dat = dat + "/";
			obj.value=dat;
		}
		else if(dat.length==10)
		{
			obj.blur();
		}
	}
	function back()
	{
		document.form1.action="gestione_account.jsp";
		document.form1.submit();
	}
</script>
<%! 
	String matricola="",username="",password="",email="",user="",qry="",dataregistrazione="",tipologia=""; 
	ResultSet rs;
	String modify;
%>
<%
	if (session.getAttribute("user")==null)
		response.sendRedirect("loginerr.jsp");
	
	modify=request.getParameter("modify").toString();
	if (modify.equalsIgnoreCase("1"))
	{
		matricola=connections.doubleQuota(request.getParameter("matricola"));
		username=connections.doubleQuota(request.getParameter("username"));
		password=connections.doubleQuota(request.getParameter("password"));
		email=connections.doubleQuota(request.getParameter("email"));
		dataregistrazione=connections.doubleQuota(request.getParameter("dataregistrazione"));
		tipologia=connections.doubleQuota(request.getParameter("tipologia"));
		
		qry = "update account set password='" + password + "',email='" + email + "',dataregistrazione=to_date('" + dataregistrazione + "','DD,MM,YYYY'),tipologia='" + tipologia.substring(0,1) +"' where username = '"+ username+"'";
		System.out.println(qry);
		utility.connections.getStatement().executeUpdate(qry);
		System.out.println("stampo");
		
	}
	user = session.getAttribute("user").toString().toLowerCase();
	username=request.getParameter("username").toString().toLowerCase();
	
	qry = "select matricola,username,password,email,to_char(dataregistrazione,'DD/MM/YYYY') as dataregistrazione, tipologia from account where username = '"+username+"'";
	rs = utility.connections.getStatement().executeQuery(qry);
	
	if (rs.next())
	{
		matricola=rs.getString("matricola");
		username=rs.getString("username");
		password=rs.getString("password");
		email=rs.getString("email");
		dataregistrazione=rs.getString("dataregistrazione");
		tipologia=rs.getString("tipologia");

	}
	else
	{
		matricola="";
		username="";
		password="";
		email="";
		dataregistrazione="";
		tipologia="";
	}
	switch(Integer.parseInt(tipologia))
	{
	case 1:
		tipologia+=",Amministratore";
		break;
	case 2:
		tipologia+=",Amministrativo";
		break;
	case 3:
		tipologia+=",Dipendente";
		break;
	}
	rs.close();
%>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>MODIFICA ACCOUNT</title>
<script type="text/javascript" src="../../js/immagini.js"></script>
<link href="../../layout.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="js/immagini.js"></script>
</head>

<body>
<table width="700" height="422" border="0" align="center">
  <tr>
    <td align="center" valign="middle"></td>
  </tr>
  <tr>
    <td height="257" align="center" valign="top"><table width="100%" border="0">
      <tr>
        <td align="center"><div align="left">
          <p class="style3">
		  </p>
        </div></td>
        </tr>
      <tr>
        <td class="style2"><p align="center"><strong>Modifica i dati </strong></p></td>
      </tr>
      
      <tr>
        <td class="style2">
          <form name="form1" method="post" action = "modifica_account.jsp?modify=1">
          <table width="100%" border="0">
            <tr>
              <td>&nbsp;</td>
              <td><div align="right">Username :</div></td>
              <td><input type="text" name="username" size="60" value="<%=username%>" readonly="readonly" ></input></td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td><div align="right">password:</div></td>
              <td><input type="text" name="password" size="60" value="<%=password%>" ></input></td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td><div align="right">e-mail:</div></td>
              <td><input type="text" name="email" size="60" value="<%=email%>" ></input></td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td><div align="right">data registrazione:</div></td>
              <td><input type="text" name="dataregistrazione" onblur="javascript:chkData('dataregistrazione');" onKeyUp="javascript:insData('dataregistrazione')" size="60" value="<%=dataregistrazione%>" ></input></td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td><div align="right">tipologia:</div></td>
              <td width="39%" class="style1"><div align="left"><label>
       		<select name="tipologia">
       		<option><%=tipologia%></option>
      		<option>1,Amministratore</option>
        	<option>2,Addetto Amministrativo</option>
        	<option>3,Dipendente</option>
       		</select>
      		</label></div></td>
        <td width="25%" class="style1">&nbsp;</td>
            </tr>
            <tr>
              <td width="16%">&nbsp;</td>
              <td width="27%"><div align="right">matricola:</div></td>
              <td width="32%"><input type="text" name="matricola" size="60" value="<%=matricola%>" readonly="readonly" ></input></td>
              <td width="25%">&nbsp;</td>
            </tr>
            <tr>
        	<td class="style3">&nbsp;</td>
        	<td class="style3">&nbsp;</td>
        	<td class="style3">
		  	<input type="button" value="MODIFICA" onclick="javascript:modify('form1');"/>
		  	<input type="button" value="INDIETRO" onclick="javascript:back();"/>
            </td>
              <td>&nbsp;</td>
              <td><div align="right"></div></td>
             </tr>
          </table>
          </form>
       		 
        </tr>
      <tr>
        <td class="style1"><div align="center"></div></td>
        </tr>
    </table></td>
  </tr>
  <tr>
    <td align="center" valign="middle">
	<%
		if(modify.equalsIgnoreCase("1"))
			out.println("RECORD MODIFICATO");
	%>
	</td>
  </tr>
  <tr>
    <td align="center" valign="middle" bgcolor="#000099"><div align="right" class="style4">MODIFICA DIPENDENTE </div></td>
  </tr>
</table>
</body>
</html>
